use actix_web::{Responder, HttpResponse, HttpRequest, Error};
use futures::future::{ready, Ready};
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use rbatis::crud::CRUD;
use rbatis::rbatis::Rbatis;
use bigdecimal::BigDecimal;
use rbatis::Error as rError;
use std::collections::HashMap;

#[crud_table( id_name:id |  id_type:String |  table_name:set_meal |  table_columns:id,room_id,name,singing_date,time_frame,time_interval,begin_time,finish_time,remark,create_time,update_time,version,delete_flag )]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SetMeal{
    pub id: Option<String>,
    pub room_id: Option<String>,
    pub name: Option<String>,
    pub singing_date: Option<String>,
    pub time_frame: Option<u32>,
    pub time_interval: Option<u32>,
    pub begin_time: Option<NaiveDateTime>,
    pub finish_time: Option<NaiveDateTime>,
    pub remark: Option<String>,
    pub create_time: Option<NaiveDateTime>,
    pub update_time: Option<NaiveDateTime>,
    pub version: Option<BigDecimal>,
    pub delete_flag: Option<i32>,
    pub start_time_list: Option<Vec<HashMap<String, String>>>,
}

impl Responder for SetMeal {
    type Error = Error;
    type Future = Ready<Result<HttpResponse, Error>>;

    fn respond_to(self, _req: &HttpRequest) -> Self::Future {
        let body = serde_json::to_string(&self).unwrap();

        // Create response and set content type
        ready(Ok(HttpResponse::Ok()
            .content_type("application/json")
            .body(body)))
    }
}

pub async fn new_set_meal() -> SetMeal{
    SetMeal{
        id: None,
        room_id: None,
        name: None,
        singing_date: None,
        time_frame: None,
        time_interval: None,
        begin_time: None,
        finish_time: None,
        remark: None,
        create_time: None,
        update_time: None,
        version: None,
        delete_flag: None,
        start_time_list: None
    }
}

impl SetMeal{

    #[sql(rbatis, "select * from set_meal where singing_date = ? and room_id = ?")]
    pub async fn find_all_set_meal(&self, rbatis: &Rbatis, singing_date :&String, room_id :&String) -> Result<Vec<SetMeal>, rError>{}

}